// import axios from "axios";
import service from "@/utils/request.js";

// 获取知识库分类信息
export const getFileLabels = () => {
  return service({
    method: "get",
    url: "/checkFile/getFileLabels",
    headers: {
      "Content-Type": "application/json",
    },
  });
};

// 获取知识库
export const getFilesByLabel = (data) => {
  return service({
    method: "post",
    url: "/checkFile/findFilesByName",
    data,
    headers: {
      "Content-Type": "application/json",
    },
  });
};

// 删除知识库
export const deleteFilesBylds = (data) => {
  const formData = new FormData();
  formData.append("ids", data.ids);

  return service({
    method: "post",
    url: "/checkFile/deleteFilesByIds",
    data: formData,
  });
};

// 新增知识库 -- 正确传递表单数据（支持文件列表）
export const uploadFiles = (formObj) => {
  const formData = new FormData();

  Object.entries(formObj).forEach(([key, value]) => {
    if (
      Array.isArray(value) &&
      value.length > 0 &&
      value[0].raw instanceof File
    ) {
      value.forEach((file) => {
        formData.append(key, file.raw, file.name);
      });
    } else if (
      typeof value === "object" &&
      value !== null &&
      !(value instanceof File)
    ) {
      formData.append(key, JSON.stringify(value));
    } else {
      formData.append(key, value);
    }
  });

  return service({
    method: "post",
    url: "/checkFile/uploadFiles",
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data", // 表单数据格式（必须）
    },
  });
};

// 解析文件

export const submitTask = (formObj) => {
  const formData = new FormData();

  Object.entries(formObj).forEach(([key, value]) => {
    // 单独处理文件字段（假设字段名为 'file'）
    if (key === "file" && value.raw instanceof File) {
      formData.append(key, value.raw, value.name); // 提取原生 File 对象
    }
    // 处理普通对象（非文件）
    else if (
      typeof value === "object" &&
      value !== null &&
      !(value instanceof File)
    ) {
      formData.append(key, JSON.stringify(value));
    }
    // 处理普通值
    else {
      formData.append(key, value);
    }
  });

  return service({
    method: "post",
    url: "/checkFile/submitTask",
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data",
    },
  });
};

// 获取知识库
export const checkresult = (fileId) => {
  return service({
    method: "get",
    // url: `/check/result/${fileId}`,
    url: `/check/result/${fileId}`,
    headers: {
      "Content-Type": "application/json",
    },
  });
};
